package com.dongdongshop.controller;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.*;
import com.dongdongshop.vo.SpecVO;
import com.dongdongshop.vo.TypeTemplateVO;
import com.dongdongsop.data.AjaxResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("goods")
public class GoodsController {

    @DubboReference
    private GoodsService goodsService;

    @DubboReference
    private ItemCatService itemCatService;

    @DubboReference
    private TypeTemplateService typeTemplateService;

    @DubboReference
    private SpecificationOptionService specificationOptionService;


    //跳转商品添加页面
    @RequestMapping("toGoodsAdd")
    public String toGoodsAdd(){
        return "admin/goods_edit";
    }

    //查询分类表
    @RequestMapping("findItemCat")
    @ResponseBody
    public AjaxResult findItemCat(@RequestParam(defaultValue = "0")Long parentId){
        try {
            List<TbItemCat> itemCatList = itemCatService.findItemCatByParentId(parentId);
            return AjaxResult.OK().setData(itemCatList);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //查询品牌表
    @RequestMapping("selectTypeTemplateByBrand")
    @ResponseBody
    public AjaxResult selectTypeTemplateByBrand(Long id){
        try {
            TbItemCat itemCatById = itemCatService.getItemCatById(id);
            TbTypeTemplate typeTemplate = typeTemplateService.getTypeTemplateByBrandAllBySpecAll(itemCatById.getTypeId());
            String specIds = typeTemplate.getSpecIds();
            List<SpecVO> specVOList = JSONObject.parseArray(specIds, SpecVO.class);
            for (SpecVO specVO : specVOList) {
                List<TbSpecificationOption> optionList = specificationOptionService.selectOptionBySpecId(specVO.getId());
                specVO.setOptionList(optionList);
            }
            TypeTemplateVO templateVO = new TypeTemplateVO();
            BeanUtils.copyProperties(typeTemplate,templateVO);
            templateVO.setSpecVOList(specVOList);
            return AjaxResult.OK().setData(templateVO);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //添加商品表和商品详细表
    @RequestMapping("addGoodsByGoodsDesc")
    @ResponseBody
    public AjaxResult addGoodsByGoodsDesc(TbGoods goods, TbGoodsDesc goodsDesc){
        try {
            Subject subject = SecurityUtils.getSubject();
            TbSeller seller = (TbSeller) subject.getPrincipal();
            goods.setSellerId(seller.getSellerId());
            goods.setAuditStatus("0");
            Long i = goodsService.addGoodsByGoodsDesc(goods,goodsDesc);
            return AjaxResult.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

}
